// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie;

import fmod.fmod.Audio;
import fmod.fmod.FMODAudio;
import fmod.fmod.FMODManager;
import fmod.fmod.FMODSoundEmitter;
import fmod.fmod.FMOD_STUDIO_EVENT_CALLBACK;
import fmod.fmod.FMOD_STUDIO_EVENT_CALLBACK_TYPE;
import fmod.fmod.FMOD_STUDIO_PARAMETER_DESCRIPTION;
import fmod.fmod.FMOD_STUDIO_PLAYBACK_STATE;
import fmod.fmod.IFMODParameterUpdater;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.HashSet;
import zombie.audio.BaseSoundEmitter;
import zombie.audio.FMODParameter;
import zombie.audio.FMODParameterList;
import zombie.audio.GameSound;
import zombie.audio.GameSoundClip;
import zombie.audio.parameters.ParameterMusicActionStyle;
import zombie.audio.parameters.ParameterMusicLibrary;
import zombie.audio.parameters.ParameterMusicState;
import zombie.audio.parameters.ParameterMusicWakeState;
import zombie.audio.parameters.ParameterMusicZombiesTargeting;
import zombie.audio.parameters.ParameterMusicZombiesVisible;
import zombie.characters.IsoPlayer;
import zombie.core.Core;
import zombie.core.Rand;
import zombie.debug.DebugLog;
import zombie.gameStates.MainScreenState;
import zombie.input.GameKeyboard;
import zombie.iso.IsoGridSquare;
import zombie.iso.IsoObject;
import zombie.iso.IsoUtils;
import zombie.iso.IsoWorld;
import zombie.network.GameClient;
import zombie.network.GameServer;
import zombie.scripting.ScriptManager;
import zombie.scripting.objects.Item;
import zombie.scripting.objects.ScriptModule;
import zombie.ui.TutorialManager;
import zombie.util.StringUtils;

public final class SoundManager extends zombie.BaseSoundManager implements IFMODParameterUpdater {
    public float SoundVolume = 0.8F;
    public float MusicVolume = 0.36F;
    public float AmbientVolume = 0.8F;
    public float VehicleEngineVolume = 0.5F;
    private final ParameterMusicActionStyle parameterMusicActionStyle = new ParameterMusicActionStyle();
    private final ParameterMusicLibrary parameterMusicLibrary = new ParameterMusicLibrary();
    private final ParameterMusicState parameterMusicState = new ParameterMusicState();
    private final ParameterMusicWakeState parameterMusicWakeState = new ParameterMusicWakeState();
    private final ParameterMusicZombiesTargeting parameterMusicZombiesTargeting = new ParameterMusicZombiesTargeting();
    private final ParameterMusicZombiesVisible parameterMusicZombiesVisible = new ParameterMusicZombiesVisible();
    private final FMODParameterList fmodParameters = new FMODParameterList();
    private boolean initialized = false;
    private long inGameGroupBus = 0L;
    private long musicGroupBus = 0L;
    private FMODSoundEmitter musicEmitter = null;
    private long musicCombinedEvent = 0L;
    private FMODSoundEmitter uiEmitter = null;
    private final zombie.SoundManager.Music music = new zombie.SoundManager.Music();
    public ArrayList<Audio> ambientPieces = new ArrayList();
    private boolean muted = false;
    private long[] bankList = new long[32];
    private long[] eventDescList = new long[256];
    private long[] eventInstList = new long[256];
    private long[] pausedEventInstances = new long[128];
    private float[] pausedEventVolumes = new float[128];
    private int pausedEventCount;
    private final HashSet<BaseSoundEmitter> emitters = new HashSet();
    private static ArrayList<zombie.SoundManager.AmbientSoundEffect> ambientSoundEffects = new ArrayList();
    public static zombie.BaseSoundManager instance;
    private String currentMusicName;
    private String currentMusicLibrary;
    private final FMOD_STUDIO_EVENT_CALLBACK musicEventCallback = new FMOD_STUDIO_EVENT_CALLBACK() {
        public void timelineMarker(long var1, String string, int _int) {
            DebugLog.Sound.debugln("timelineMarker %s %d", string, _int);
            if ("Lightning".equals(string)) {
                MainScreenState.getInstance().lightningTimelineMarker = true;
            }
        }
    };

    public FMODParameterList getFMODParameters() {
        return this.fmodParameters;
    }

    public void startEvent(long eventInstance, GameSoundClip clip, BitSet parameterSet) {
        FMODParameterList fMODParameterList = this.getFMODParameters();
        ArrayList arrayList = clip.eventDescription.parameters;

        for (int _int = 0; _int < arrayList.size(); _int++) {
            FMOD_STUDIO_PARAMETER_DESCRIPTION fMOD_STUDIO_PARAMETER_DESCRIPTION = (FMOD_STUDIO_PARAMETER_DESCRIPTION)arrayList.get(_int);
            if (!parameterSet.get(fMOD_STUDIO_PARAMETER_DESCRIPTION.globalIndex)) {
                FMODParameter fMODParameter = fMODParameterList.get(fMOD_STUDIO_PARAMETER_DESCRIPTION);
                if (fMODParameter != null) {
                    fMODParameter.startEventInstance(eventInstance);
                }
            }
        }
    }

    public void updateEvent(long eventInstance, GameSoundClip clip) {
    }

    public void stopEvent(long eventInstance, GameSoundClip clip, BitSet parameterSet) {
        FMODParameterList fMODParameterList = this.getFMODParameters();
        ArrayList arrayList = clip.eventDescription.parameters;

        for (int _int = 0; _int < arrayList.size(); _int++) {
            FMOD_STUDIO_PARAMETER_DESCRIPTION fMOD_STUDIO_PARAMETER_DESCRIPTION = (FMOD_STUDIO_PARAMETER_DESCRIPTION)arrayList.get(_int);
            if (!parameterSet.get(fMOD_STUDIO_PARAMETER_DESCRIPTION.globalIndex)) {
                FMODParameter fMODParameter = fMODParameterList.get(fMOD_STUDIO_PARAMETER_DESCRIPTION);
                if (fMODParameter != null) {
                    fMODParameter.stopEventInstance(eventInstance);
                }
            }
        }
    }

    @Override
    public boolean isRemastered() {
        int _int = Core.getInstance().getOptionMusicLibrary();
        return _int == 1 || _int == 3 && Rand.Next(2) == 0;
    }

    @Override
    public void BlendVolume(Audio var1, float var2) {
    }

    @Override
    public void BlendVolume(Audio var1, float var2, float var3) {
    }

    @Override
    public Audio BlendThenStart(Audio var1, float var2, String var3) {
        return null;
    }

    @Override
    public void FadeOutMusic(String name, int milli) {
    }

    @Override
    public void PlayAsMusic(String var1, Audio var2, float var3, boolean var4) {
    }

    @Override
    public long playUISound(String name) {
        GameSound gameSound = zombie.GameSounds.getSound(name);
        if (gameSound != null && !gameSound.clips.isEmpty()) {
            GameSoundClip gameSoundClip = gameSound.getRandomClip();
            long _long = this.uiEmitter.playClip(gameSoundClip, null);
            this.uiEmitter.tick();
            fmod.javafmod.FMOD_System_Update();
            return _long;
        } else {
            return 0L;
        }
    }

    @Override
    public boolean isPlayingUISound(String name) {
        return this.uiEmitter.isPlaying(name);
    }

    @Override
    public boolean isPlayingUISound(long eventInstance) {
        return this.uiEmitter.isPlaying(eventInstance);
    }

    @Override
    public void stopUISound(long eventInstance) {
        this.uiEmitter.stopSound(eventInstance);
    }

    @Override
    public boolean IsMusicPlaying() {
        return false;
    }

    @Override
    public boolean isPlayingMusic() {
        return this.music.isPlaying();
    }

    @Override
    public ArrayList<Audio> getAmbientPieces() {
        return this.ambientPieces;
    }

    private void gatherInGameEventInstances() {
        this.pausedEventCount = 0;
        int int0 = fmod.javafmodJNI.FMOD_Studio_System_GetBankCount();
        if (this.bankList.length < int0) {
            this.bankList = new long[int0];
        }

        int0 = fmod.javafmodJNI.FMOD_Studio_System_GetBankList(this.bankList);

        for (int int1 = 0; int1 < int0; int1++) {
            int int2 = fmod.javafmodJNI.FMOD_Studio_Bank_GetEventCount(this.bankList[int1]);
            if (this.eventDescList.length < int2) {
                this.eventDescList = new long[int2];
            }

            int2 = fmod.javafmodJNI.FMOD_Studio_Bank_GetEventList(this.bankList[int1], this.eventDescList);

            for (int int3 = 0; int3 < int2; int3++) {
                int int4 = fmod.javafmodJNI.FMOD_Studio_EventDescription_GetInstanceCount(this.eventDescList[int3]);
                if (this.eventInstList.length < int4) {
                    this.eventInstList = new long[int4];
                }

                int4 = fmod.javafmodJNI.FMOD_Studio_EventDescription_GetInstanceList(this.eventDescList[int3], this.eventInstList);

                for (int int5 = 0; int5 < int4; int5++) {
                    int int6 = fmod.javafmod.FMOD_Studio_GetPlaybackState(this.eventInstList[int5]);
                    if (int6 != FMOD_STUDIO_PLAYBACK_STATE.FMOD_STUDIO_PLAYBACK_STOPPED.index) {
                        boolean _boolean = fmod.javafmodJNI.FMOD_Studio_EventInstance_GetPaused(this.eventInstList[int5]);
                        if (!_boolean) {
                            if (this.pausedEventInstances.length < this.pausedEventCount + 1) {
                                this.pausedEventInstances = Arrays.copyOf(this.pausedEventInstances, this.pausedEventCount + 128);
                                this.pausedEventVolumes = Arrays.copyOf(this.pausedEventVolumes, this.pausedEventInstances.length);
                            }

                            this.pausedEventInstances[this.pausedEventCount] = this.eventInstList[int5];
                            this.pausedEventVolumes[this.pausedEventCount] = fmod.javafmodJNI.FMOD_Studio_EventInstance_GetVolume(this.eventInstList[int5]);
                            this.pausedEventCount++;
                        }
                    }
                }
            }
        }
    }

    @Override
    public void pauseSoundAndMusic() {
        boolean _boolean = true;
        if (GameClient.bClient) {
            this.muted = true;
            if (_boolean) {
                fmod.javafmod.FMOD_Studio_Bus_SetMute(this.inGameGroupBus, true);
                fmod.javafmod.FMOD_Studio_Bus_SetMute(this.musicGroupBus, true);
            } else {
                this.setSoundVolume(0.0F);
                this.setMusicVolume(0.0F);
                this.setAmbientVolume(0.0F);
                this.setVehicleEngineVolume(0.0F);
            }

            zombie.GameSounds.soundIsPaused = true;
        } else if (_boolean) {
            fmod.javafmod.FMOD_Studio_Bus_SetPaused(this.inGameGroupBus, true);
            fmod.javafmod.FMOD_Studio_Bus_SetPaused(this.musicGroupBus, true);
            fmod.javafmod.FMOD_Channel_SetPaused(FMODManager.instance.channelGroupInGameNonBankSounds, true);
            zombie.GameSounds.soundIsPaused = true;
        } else {
            long _long = fmod.javafmod.FMOD_System_GetMasterChannelGroup();
            fmod.javafmod.FMOD_ChannelGroup_SetPaused(_long, true);
            fmod.javafmod.FMOD_ChannelGroup_SetVolume(_long, 0.0F);
            fmod.javafmodJNI.FMOD_Studio_System_FlushCommands();
            this.gatherInGameEventInstances();

            for (int _int = 0; _int < this.pausedEventCount; _int++) {
                fmod.javafmodJNI.FMOD_Studio_EventInstance_SetPaused(this.pausedEventInstances[_int], true);
            }

            fmod.javafmod.FMOD_Channel_SetPaused(FMODManager.instance.channelGroupInGameNonBankSounds, true);
            fmod.javafmod.FMOD_ChannelGroup_SetPaused(_long, false);
            fmod.javafmodJNI.FMOD_Studio_System_FlushCommands();
            fmod.javafmod.FMOD_ChannelGroup_SetVolume(_long, 1.0F);
            zombie.GameSounds.soundIsPaused = true;
        }
    }

    @Override
    public void resumeSoundAndMusic() {
        boolean _boolean = true;
        if (this.muted) {
            this.muted = false;
            if (_boolean) {
                fmod.javafmod.FMOD_Studio_Bus_SetMute(this.inGameGroupBus, false);
                fmod.javafmod.FMOD_Studio_Bus_SetMute(this.musicGroupBus, false);
                fmod.javafmod.FMOD_ChannelGroup_SetPaused(FMODManager.instance.channelGroupInGameNonBankSounds, false);
            } else {
                this.setSoundVolume((float)Core.getInstance().getOptionSoundVolume() / 10.0F);
                this.setMusicVolume((float)Core.getInstance().getOptionMusicVolume() / 10.0F);
                this.setAmbientVolume((float)Core.getInstance().getOptionAmbientVolume() / 10.0F);
                this.setVehicleEngineVolume((float)Core.getInstance().getOptionVehicleEngineVolume() / 10.0F);
            }

            zombie.GameSounds.soundIsPaused = false;
        } else if (_boolean) {
            fmod.javafmod.FMOD_Studio_Bus_SetPaused(this.inGameGroupBus, false);
            fmod.javafmod.FMOD_Studio_Bus_SetPaused(this.musicGroupBus, false);
            fmod.javafmod.FMOD_ChannelGroup_SetPaused(FMODManager.instance.channelGroupInGameNonBankSounds, false);
            zombie.GameSounds.soundIsPaused = false;
        } else {
            long _long = fmod.javafmod.FMOD_System_GetMasterChannelGroup();
            fmod.javafmod.FMOD_ChannelGroup_SetPaused(_long, true);
            fmod.javafmodJNI.FMOD_Studio_System_FlushCommands();

            for (int _int = 0; _int < this.pausedEventCount; _int++) {
                try {
                    fmod.javafmodJNI.FMOD_Studio_EventInstance_SetPaused(this.pausedEventInstances[_int], false);
                } catch (Throwable throwable) {
                    throwable.printStackTrace();
                }
            }

            this.pausedEventCount = 0;
            fmod.javafmod.FMOD_ChannelGroup_SetPaused(_long, false);
            fmod.javafmod.FMOD_ChannelGroup_SetVolume(_long, 1.0F);
            fmod.javafmod.FMOD_ChannelGroup_SetPaused(FMODManager.instance.channelGroupInGameNonBankSounds, false);
            zombie.GameSounds.soundIsPaused = false;
        }
    }

    private void debugScriptSound(Item item, String string) {
        if (string != null && !string.isEmpty()) {
            if (!zombie.GameSounds.isKnownSound(string)) {
                DebugLog.General.warn("no such sound \"" + string + "\" in item " + item.getFullName());
            }
        }
    }

    @Override
    public void debugScriptSounds() {
        if (Core.bDebug) {
            for (ScriptModule scriptModule : ScriptManager.instance.ModuleMap.values()) {
                for (Item item : scriptModule.ItemMap.values()) {
                    this.debugScriptSound(item, item.getBreakSound());
                    this.debugScriptSound(item, item.getBulletOutSound());
                    this.debugScriptSound(item, item.getCloseSound());
                    this.debugScriptSound(item, item.getCustomEatSound());
                    this.debugScriptSound(item, item.getDoorHitSound());
                    this.debugScriptSound(item, item.getCountDownSound());
                    this.debugScriptSound(item, item.getExplosionSound());
                    this.debugScriptSound(item, item.getImpactSound());
                    this.debugScriptSound(item, item.getOpenSound());
                    this.debugScriptSound(item, item.getPutInSound());
                    this.debugScriptSound(item, item.getPlaceOneSound());
                    this.debugScriptSound(item, item.getPlaceMultipleSound());
                    this.debugScriptSound(item, item.getShellFallSound());
                    this.debugScriptSound(item, item.getSwingSound());
                    this.debugScriptSound(item, item.getInsertAmmoSound());
                    this.debugScriptSound(item, item.getInsertAmmoStartSound());
                    this.debugScriptSound(item, item.getInsertAmmoStopSound());
                    this.debugScriptSound(item, item.getEjectAmmoSound());
                    this.debugScriptSound(item, item.getEjectAmmoStartSound());
                    this.debugScriptSound(item, item.getEjectAmmoStopSound());
                }
            }
        }
    }

    @Override
    public void registerEmitter(BaseSoundEmitter emitter) {
        this.emitters.add(emitter);
    }

    @Override
    public void unregisterEmitter(BaseSoundEmitter emitter) {
        this.emitters.remove(emitter);
    }

    @Override
    public boolean isListenerInRange(float x, float y, float range) {
        if (GameServer.bServer) {
            return false;
        } else {
            for (int _int = 0; _int < IsoPlayer.numPlayers; _int++) {
                IsoPlayer isoPlayer = IsoPlayer.players[_int];
                if (isoPlayer != null && !isoPlayer.Traits.Deaf.isSet() && IsoUtils.DistanceToSquared(isoPlayer.x, isoPlayer.y, x, y) < range * range) {
                    return true;
                }
            }

            return false;
        }
    }

    @Override
    public void playNightAmbient(String choice) {
        DebugLog.log("playNightAmbient: " + choice);

        for (int _int = 0; _int < ambientSoundEffects.size(); _int++) {
            zombie.SoundManager.AmbientSoundEffect ambientSoundEffect0 = (zombie.SoundManager.AmbientSoundEffect)ambientSoundEffects.get(_int);
            if (ambientSoundEffect0.getName().equals(choice)) {
                ambientSoundEffect0.setVolume((float)Rand.Next(700, 1500) / 1000.0F);
                ambientSoundEffect0.start();
                this.ambientPieces.add(ambientSoundEffect0);
                return;
            }
        }

        zombie.SoundManager.AmbientSoundEffect ambientSoundEffect1 = new zombie.SoundManager.AmbientSoundEffect(choice);
        ambientSoundEffect1.setVolume((float)Rand.Next(700, 1500) / 1000.0F);
        ambientSoundEffect1.setName(choice);
        ambientSoundEffect1.start();
        this.ambientPieces.add(ambientSoundEffect1);
        ambientSoundEffects.add(ambientSoundEffect1);
    }

    @Override
    public void playMusic(String name) {
        this.DoMusic(name, false);
    }

    @Override
    public void playAmbient(String name) {
    }

    @Override
    public void playMusicNonTriggered(String name, float gain) {
    }

    @Override
    public void stopMusic(String name) {
        if (this.isPlayingMusic()) {
            if (StringUtils.isNullOrWhitespace(name) || name.equalsIgnoreCase(this.getCurrentMusicName())) {
                this.StopMusic();
            }
        }
    }

    @Override
    public void CheckDoMusic() {
    }

    @Override
    public float getMusicPosition() {
        return this.isPlayingMusic() ? this.music.getPosition() : 0.0F;
    }

    @Override
    public void DoMusic(String name, boolean bLoop) {
        if (this.AllowMusic && Core.getInstance().getOptionMusicVolume() != 0) {
            if (this.isPlayingMusic()) {
                this.StopMusic();
            }

            int _int = Core.getInstance().getOptionMusicLibrary();
            boolean _boolean = _int == 1;
            GameSound gameSound = zombie.GameSounds.getSound(name);
            GameSoundClip gameSoundClip = null;
            if (gameSound != null && !gameSound.clips.isEmpty()) {
                gameSoundClip = gameSound.getRandomClip();
            }

            if (gameSoundClip != null && gameSoundClip.getEvent() != null) {
                if (gameSoundClip.eventDescription != null) {
                    long long0 = gameSoundClip.eventDescription.address;
                    fmod.javafmod.FMOD_Studio_LoadEventSampleData(long0);
                    this.music.instance = fmod.javafmod.FMOD_Studio_System_CreateEventInstance(long0);
                    this.music.clip = gameSoundClip;
                    this.music.effectiveVolume = gameSoundClip.getEffectiveVolume();
                    fmod.javafmod.FMOD_Studio_EventInstance_SetParameterByName(this.music.instance, "Volume", 10.0F);
                    fmod.javafmod.FMOD_Studio_EventInstance_SetVolume(this.music.instance, this.music.effectiveVolume);
                    fmod.javafmod.FMOD_Studio_StartEvent(this.music.instance);
                }
            } else if (gameSoundClip != null && gameSoundClip.getFile() != null) {
                long long1 = FMODManager.instance.loadSound(gameSoundClip.getFile());
                if (long1 > 0L) {
                    this.music.channel = fmod.javafmod.FMOD_System_PlaySound(long1, true);
                    this.music.clip = gameSoundClip;
                    this.music.effectiveVolume = gameSoundClip.getEffectiveVolume();
                    fmod.javafmod.FMOD_Channel_SetVolume(this.music.channel, this.music.effectiveVolume);
                    fmod.javafmod.FMOD_Channel_SetPitch(this.music.channel, gameSoundClip.pitch);
                    fmod.javafmod.FMOD_Channel_SetPaused(this.music.channel, false);
                }
            }

            this.currentMusicName = name;
            this.currentMusicLibrary = _boolean ? "official" : "earlyaccess";
        }
    }

    @Override
    public void PlayAsMusic(String var1, Audio var2, boolean var3, float var4) {
    }

    @Override
    public void setMusicState(String stateName) {
        switch (stateName) {
            case "MainMenu":
                this.parameterMusicState.setState(ParameterMusicState.State.MainMenu);
                break;
            case "Loading":
                this.parameterMusicState.setState(ParameterMusicState.State.Loading);
                break;
            case "InGame":
                this.parameterMusicState.setState(ParameterMusicState.State.InGame);
                break;
            case "PauseMenu":
                this.parameterMusicState.setState(ParameterMusicState.State.PauseMenu);
                break;
            case "Tutorial":
                this.parameterMusicState.setState(ParameterMusicState.State.Tutorial);
                break;
            default:
                DebugLog.General.warn("unknown MusicState \"%s\"", stateName);
        }
    }

    @Override
    public void setMusicWakeState(IsoPlayer player, String stateName) {
        switch (stateName) {
            case "Awake":
                this.parameterMusicWakeState.setState(player, ParameterMusicWakeState.State.Awake);
                break;
            case "Sleeping":
                this.parameterMusicWakeState.setState(player, ParameterMusicWakeState.State.Sleeping);
                break;
            case "WakeNormal":
                this.parameterMusicWakeState.setState(player, ParameterMusicWakeState.State.WakeNormal);
                break;
            case "WakeNightmare":
                this.parameterMusicWakeState.setState(player, ParameterMusicWakeState.State.WakeNightmare);
                break;
            case "WakeZombies":
                this.parameterMusicWakeState.setState(player, ParameterMusicWakeState.State.WakeZombies);
                break;
            default:
                DebugLog.General.warn("unknown MusicWakeState \"%s\"", stateName);
        }
    }

    @Override
    public Audio PlayMusic(String var1, String var2, boolean var3, float var4) {
        return null;
    }

    @Override
    public Audio PlaySound(String var1, boolean var2, float var3, float var4) {
        return null;
    }

    @Override
    public Audio PlaySound(String string, boolean var2, float var3) {
        if (GameServer.bServer) {
            return null;
        } else if (IsoWorld.instance == null) {
            return null;
        } else {
            BaseSoundEmitter baseSoundEmitter = IsoWorld.instance.getFreeEmitter();
            baseSoundEmitter.setPos(0.0F, 0.0F, 0.0F);
            long _long = baseSoundEmitter.playSound(string);
            return _long != 0L ? new FMODAudio(baseSoundEmitter) : null;
        }
    }

    @Override
    public Audio PlaySoundEvenSilent(String var1, boolean var2, float var3) {
        return null;
    }

    @Override
    public Audio PlayJukeboxSound(String var1, boolean var2, float var3) {
        return null;
    }

    @Override
    public Audio PlaySoundWav(String var1, boolean var2, float var3, float var4) {
        return null;
    }

    @Override
    public Audio PlaySoundWav(String var1, boolean var2, float var3) {
        return null;
    }

    @Override
    public Audio PlaySoundWav(String var1, int var2, boolean var3, float var4) {
        return null;
    }

    @Override
    public void update3D() {
    }

    @Override
    public Audio PlayWorldSound(String string, IsoGridSquare isoGridSquare, float float0, float float1, float float2, boolean _boolean) {
        return this.PlayWorldSound(string, false, isoGridSquare, float0, float1, float2, _boolean);
    }

    @Override
    public Audio PlayWorldSound(String string, boolean boolean0, IsoGridSquare isoGridSquare, float float0, float float1, float float2, boolean boolean1) {
        if (!GameServer.bServer && isoGridSquare != null) {
            if (GameClient.bClient) {
                GameClient.instance.PlayWorldSound(string, isoGridSquare.x, isoGridSquare.y, (byte)isoGridSquare.z);
            }

            return this.PlayWorldSoundImpl(string, boolean0, isoGridSquare.getX(), isoGridSquare.getY(), isoGridSquare.getZ(), float0, float1, float2, boolean1);
        } else {
            return null;
        }
    }

    @Override
    public Audio PlayWorldSoundImpl(String string, boolean var2, int int2, int int1, int int0, float var6, float var7, float var8, boolean var9) {
        BaseSoundEmitter baseSoundEmitter = IsoWorld.instance.getFreeEmitter((float)int2 + 0.5F, (float)int1 + 0.5F, (float)int0);
        long _long = baseSoundEmitter.playSoundImpl(string, (IsoObject)null);
        return new FMODAudio(baseSoundEmitter);
    }

    @Override
    public Audio PlayWorldSound(String string, IsoGridSquare isoGridSquare, float float0, float float1, float float2, int var6, boolean _boolean) {
        return this.PlayWorldSound(string, isoGridSquare, float0, float1, float2, _boolean);
    }

    @Override
    public Audio PlayWorldSoundWav(String string, IsoGridSquare isoGridSquare, float float0, float float1, float float2, boolean _boolean) {
        return this.PlayWorldSoundWav(string, false, isoGridSquare, float0, float1, float2, _boolean);
    }

    @Override
    public Audio PlayWorldSoundWav(String string, boolean boolean0, IsoGridSquare isoGridSquare, float float0, float float1, float float2, boolean boolean1) {
        if (!GameServer.bServer && isoGridSquare != null) {
            if (GameClient.bClient) {
                GameClient.instance.PlayWorldSound(string, isoGridSquare.getX(), isoGridSquare.getY(), (byte)isoGridSquare.getZ());
            }

            return this.PlayWorldSoundWavImpl(string, boolean0, isoGridSquare, float0, float1, float2, boolean1);
        } else {
            return null;
        }
    }

    @Override
    public Audio PlayWorldSoundWavImpl(String string, boolean var2, IsoGridSquare isoGridSquare, float var4, float var5, float var6, boolean var7) {
        BaseSoundEmitter baseSoundEmitter = IsoWorld.instance
            .getFreeEmitter((float)isoGridSquare.getX() + 0.5F, (float)isoGridSquare.getY() + 0.5F, (float)isoGridSquare.getZ());
        baseSoundEmitter.playSound(string);
        return new FMODAudio(baseSoundEmitter);
    }

    @Override
    public void PlayWorldSoundWav(String name, IsoGridSquare source, float pitchVar, float radius, float maxGain, int choices, boolean ignoreOutside) {
        Integer integer = Rand.Next(choices) + 1;
        this.PlayWorldSoundWav(name + integer.toString(), source, pitchVar, radius, maxGain, ignoreOutside);
    }

    @Override
    public Audio PrepareMusic(String var1) {
        return null;
    }

    @Override
    public Audio Start(Audio var1, float var2, String var3) {
        return null;
    }

    @Override
    public void Update() {
        if (!this.initialized) {
            this.initialized = true;
            this.inGameGroupBus = fmod.javafmod.FMOD_Studio_System_GetBus("bus:/InGame");
            this.musicGroupBus = fmod.javafmod.FMOD_Studio_System_GetBus("bus:/Music");
            this.musicEmitter = new FMODSoundEmitter();
            this.unregisterEmitter(this.musicEmitter);
            this.musicEmitter.parameterUpdater = this;
            this.fmodParameters.add(this.parameterMusicActionStyle);
            this.fmodParameters.add(this.parameterMusicLibrary);
            this.fmodParameters.add(this.parameterMusicState);
            this.fmodParameters.add(this.parameterMusicWakeState);
            this.fmodParameters.add(this.parameterMusicZombiesTargeting);
            this.fmodParameters.add(this.parameterMusicZombiesVisible);
            this.uiEmitter = new FMODSoundEmitter();
        }

        FMODSoundEmitter.update();
        this.updateMusic();
        this.uiEmitter.tick();

        for (int _int = 0; _int < this.ambientPieces.size(); _int++) {
            Audio audio = (Audio)this.ambientPieces.get(_int);
            if (IsoPlayer.allPlayersDead()) {
                audio.stop();
            }

            if (!audio.isPlaying()) {
                audio.stop();
                this.ambientPieces.remove(audio);
                _int--;
            } else if (audio instanceof zombie.SoundManager.AmbientSoundEffect) {
                ((zombie.SoundManager.AmbientSoundEffect)audio).update();
            }
        }

        zombie.AmbientStreamManager.instance.update();
        if (!this.AllowMusic) {
            this.StopMusic();
        }

        if (this.music.isPlaying()) {
            this.music.update();
        }

        FMODManager.instance.tick();
    }

    @Override
    protected boolean HasMusic(Audio var1) {
        return false;
    }

    @Override
    public void Purge() {
    }

    @Override
    public void stop() {
        for (BaseSoundEmitter baseSoundEmitter : this.emitters) {
            baseSoundEmitter.stopAll();
        }

        this.emitters.clear();
        long _long = fmod.javafmod.FMOD_System_GetMasterChannelGroup();
        fmod.javafmod.FMOD_ChannelGroup_Stop(_long);
        this.pausedEventCount = 0;
    }

    @Override
    public void StopMusic() {
        this.music.stop();
    }

    @Override
    public void StopSound(Audio audio) {
        audio.stop();
    }

    @Override
    public void CacheSound(String file) {
    }

    @Override
    public void update4() {
    }

    @Override
    public void update2() {
    }

    @Override
    public void update3() {
    }

    @Override
    public void update1() {
    }

    @Override
    public void setSoundVolume(float volume) {
        this.SoundVolume = volume;
    }

    @Override
    public float getSoundVolume() {
        return this.SoundVolume;
    }

    @Override
    public void setAmbientVolume(float volume) {
        this.AmbientVolume = volume;
    }

    @Override
    public float getAmbientVolume() {
        return this.AmbientVolume;
    }

    @Override
    public void setMusicVolume(float volume) {
        this.MusicVolume = volume;
        if (!this.muted) {
            ;
        }
    }

    @Override
    public float getMusicVolume() {
        return this.MusicVolume;
    }

    @Override
    public void setVehicleEngineVolume(float volume) {
        this.VehicleEngineVolume = volume;
    }

    @Override
    public float getVehicleEngineVolume() {
        return this.VehicleEngineVolume;
    }

    @Override
    public String getCurrentMusicName() {
        return this.isPlayingMusic() ? this.currentMusicName : null;
    }

    @Override
    public String getCurrentMusicLibrary() {
        return this.isPlayingMusic() ? this.currentMusicLibrary : null;
    }

    private void updateMusic() {
        this.fmodParameters.update();
        if (GameKeyboard.isKeyPressed(Core.getInstance().getKey("Toggle Music"))) {
            this.AllowMusic = !this.AllowMusic;
            if (!this.AllowMusic) {
                this.StopMusic();
                TutorialManager.instance.PrefMusic = null;
            }
        }

        if (!this.musicEmitter.isPlaying(this.musicCombinedEvent)) {
            this.musicCombinedEvent = this.musicEmitter.playSoundImpl("MusicCombined", (IsoObject)null);
            if (this.musicCombinedEvent != 0L && !System.getProperty("os.name").contains("OS X")) {
                fmod.javafmod.FMOD_Studio_EventInstance_SetCallback(
                    this.musicCombinedEvent, this.musicEventCallback, FMOD_STUDIO_EVENT_CALLBACK_TYPE.FMOD_STUDIO_EVENT_CALLBACK_TIMELINE_MARKER.bit
                );
            }
        }

        if (this.musicEmitter.isPlaying(this.musicCombinedEvent)) {
            this.musicEmitter.setVolume(this.musicCombinedEvent, this.AllowMusic ? 1.0F : 0.0F);
        }

        this.musicEmitter.tick();
    }

    public static final class AmbientSoundEffect implements Audio {
        public String name;
        public long eventInstance;
        public float gain;
        public GameSoundClip clip;
        public float effectiveVolume;

        public AmbientSoundEffect(String _name) {
            GameSound gameSound = zombie.GameSounds.getSound(_name);
            if (gameSound != null && !gameSound.clips.isEmpty()) {
                GameSoundClip gameSoundClip = gameSound.getRandomClip();
                if (gameSoundClip.getEvent() != null) {
                    if (gameSoundClip.eventDescription != null) {
                        this.eventInstance = fmod.javafmod.FMOD_Studio_System_CreateEventInstance(gameSoundClip.eventDescription.address);
                        if (this.eventInstance >= 0L) {
                            this.clip = gameSoundClip;
                        }
                    }
                }
            }
        }

        public void setVolume(float volume) {
            if (this.eventInstance > 0L) {
                this.gain = volume;
                this.effectiveVolume = this.clip.getEffectiveVolume();
                fmod.javafmod.FMOD_Studio_EventInstance_SetVolume(this.eventInstance, this.gain * this.effectiveVolume);
            }
        }

        public void start() {
            if (this.eventInstance > 0L) {
                fmod.javafmod.FMOD_Studio_StartEvent(this.eventInstance);
            }
        }

        public void pause() {
        }

        public void stop() {
            DebugLog.log("stop ambient " + this.name);
            if (this.eventInstance > 0L) {
                fmod.javafmod.FMOD_Studio_EventInstance_Stop(this.eventInstance, false);
            }
        }

        public boolean isPlaying() {
            if (this.eventInstance <= 0L) {
                return false;
            } else {
                int _int = fmod.javafmod.FMOD_Studio_GetPlaybackState(this.eventInstance);
                return _int == FMOD_STUDIO_PLAYBACK_STATE.FMOD_STUDIO_PLAYBACK_STARTING.index
                    || _int == FMOD_STUDIO_PLAYBACK_STATE.FMOD_STUDIO_PLAYBACK_PLAYING.index
                    || _int == FMOD_STUDIO_PLAYBACK_STATE.FMOD_STUDIO_PLAYBACK_SUSTAINING.index;
            }
        }

        public void setName(String choice) {
            this.name = choice;
        }

        public String getName() {
            return this.name;
        }

        public void update() {
            if (this.clip != null) {
                this.clip = this.clip.checkReloaded();
                float _float = this.clip.getEffectiveVolume();
                if (this.effectiveVolume != _float) {
                    this.effectiveVolume = _float;
                    fmod.javafmod.FMOD_Studio_EventInstance_SetVolume(this.eventInstance, this.gain * this.effectiveVolume);
                }
            }
        }
    }

    private static final class Music {
        public GameSoundClip clip;
        public long instance;
        public long channel;
        public long sound;
        public float effectiveVolume;

        public boolean isPlaying() {
            if (this.instance != 0L) {
                int _int = fmod.javafmod.FMOD_Studio_GetPlaybackState(this.instance);
                return _int != FMOD_STUDIO_PLAYBACK_STATE.FMOD_STUDIO_PLAYBACK_STOPPED.index
                    && _int != FMOD_STUDIO_PLAYBACK_STATE.FMOD_STUDIO_PLAYBACK_STOPPING.index;
            } else {
                return this.channel != 0L && fmod.javafmod.FMOD_Channel_IsPlaying(this.channel);
            }
        }

        public void update() {
            this.clip = this.clip.checkReloaded();
            float _float = this.clip.getEffectiveVolume();
            if (this.effectiveVolume != _float) {
                this.effectiveVolume = _float;
                if (this.instance != 0L) {
                    fmod.javafmod.FMOD_Studio_EventInstance_SetVolume(this.instance, this.effectiveVolume);
                }

                if (this.channel != 0L) {
                    fmod.javafmod.FMOD_Channel_SetVolume(this.channel, this.effectiveVolume);
                }
            }
        }

        public float getPosition() {
            if (this.instance != 0L) {
                long long0 = fmod.javafmod.FMOD_Studio_GetTimelinePosition(this.instance);
                return (float)long0;
            } else if (this.channel != 0L) {
                long long1 = fmod.javafmod.FMOD_Channel_GetPosition(this.channel, FMODManager.FMOD_TIMEUNIT_MS);
                return (float)long1;
            } else {
                return 0.0F;
            }
        }

        public void stop() {
            if (this.instance != 0L) {
                fmod.javafmod.FMOD_Studio_EventInstance_Stop(this.instance, false);
                fmod.javafmod.FMOD_Studio_ReleaseEventInstance(this.instance);
                this.instance = 0L;
            }

            if (this.channel != 0L) {
                fmod.javafmod.FMOD_Channel_Stop(this.channel);
                this.channel = 0L;
                fmod.javafmod.FMOD_Sound_Release(this.sound);
                this.sound = 0L;
            }
        }
    }
}
